import { ref } from 'vue'
import { defineStore } from 'pinia'
import router from '../../router'

// 菜单项接口
interface MenuItem {
  id: string
  name: string
  path: string
  icon?: string
  children?: MenuItem[]
  meta?: {
    title: string
    requiresAuth?: boolean
    hidden?: boolean
  }
}

export const useMenuStore = defineStore('menu', () => {
  // 菜单列表
  const menus = ref<MenuItem[]>([])
  
  // 激活的菜单项
  const activeMenu = ref<string>('')
  
  // 面包屑路径
  const breadcrumbs = ref<MenuItem[]>([])
  
  // 初始化菜单
  const initMenus = () => {
    // 在实际项目中，这里应该从 API 获取菜单数据
    // 这里使用模拟数据进行演示
    menus.value = [
      {
        id: 'dashboard',
        name: 'Dashboard',
        path: '/dashboard',
        icon: 'pi pi-home',
        meta: {
          title: '仪表盘',
          requiresAuth: true
        }
      },
      {
        id: 'system',
        name: 'System',
        path: '/system',
        icon: 'pi pi-cog',
        meta: {
          title: '系统管理',
          requiresAuth: true
        },
        children: [
          {
            id: 'user-list',
            name: 'UserList',
            path: '/system/user/list',
            icon: 'pi pi-users',
            meta: {
              title: '用户管理',
              requiresAuth: true
            }
          },
          {
            id: 'dept-list',
            name: 'DeptList',
            path: '/system/dept/list',
            icon: 'pi pi-building',
            meta: {
              title: '部门管理',
              requiresAuth: true
            }
          },
          {
            id: 'role-list',
            name: 'RoleList',
            path: '/system/role/list',
            icon: 'pi pi-key',
            meta: {
              title: '角色管理',
              requiresAuth: true
            }
          },
          {
            id: 'post-list',
            name: 'PostList',
            path: '/system/post/list',
            icon: 'pi pi-briefcase',
            meta: {
              title: '岗位管理',
              requiresAuth: true
            }
          },
          {
            id: 'menu-list',
            name: 'MenuList',
            path: '/system/menu/list',
            icon: 'pi pi-list',
            meta: {
              title: '菜单管理',
              requiresAuth: true
            }
          },
          {
            id: 'log-list',
            name: 'LogList',
            path: '/system/log/list',
            icon: 'pi pi-file',
            meta: {
              title: '日志管理',
              requiresAuth: true
            }
          },
          {
            id: 'org-list',
            name: 'OrgList',
            path: '/system/org/list',
            icon: 'pi pi-sitemap',
            meta: {
              title: '组织架构',
              requiresAuth: true
            }
          }
        ]
      },
      {
        id: 'workflow',
        name: 'Workflow',
        path: '/workflow',
        icon: 'pi pi-share-alt',
        meta: {
          title: '流程管理',
          requiresAuth: true
        },
        children: [
          {
            id: 'workflow-category',
            name: 'WorkflowCategory',
            path: '/workflow/category/list',
            icon: 'pi pi-tags',
            meta: {
              title: '流程分类',
              requiresAuth: true
            }
          },
          {
            id: 'workflow-definition',
            name: 'WorkflowDefinition',
            path: '/workflow/definition/list',
            icon: 'pi pi-file-edit',
            meta: {
              title: '流程定义',
              requiresAuth: true
            }
          },
          {
            id: 'workflow-instance',
            name: 'WorkflowInstance',
            path: '/workflow/instance/list',
            icon: 'pi pi-clone',
            meta: {
              title: '流程实例',
              requiresAuth: true
            }
          },
          {
            id: 'workflow-history',
            name: 'WorkflowHistory',
            path: '/workflow/history/list',
            icon: 'pi pi-history',
            meta: {
              title: '历史流程',
              requiresAuth: true
            }
          }
        ]
      },
      {
        id: 'task',
        name: 'Task',
        path: '/task',
        icon: 'pi pi-check-square',
        meta: {
          title: '任务管理',
          requiresAuth: true
        },
        children: [
          {
            id: 'task-todo',
            name: 'TaskTodo',
            path: '/task/todo/list',
            icon: 'pi pi-inbox',
            meta: {
              title: '待办任务',
              requiresAuth: true
            }
          },
          {
            id: 'task-done',
            name: 'TaskDone',
            path: '/task/done/list',
            icon: 'pi pi-check-circle',
            meta: {
              title: '已办任务',
              requiresAuth: true
            }
          }
        ]
      },
      {
        id: 'settings',
        name: 'Settings',
        path: '/settings',
        icon: 'pi pi-cog',
        meta: {
          title: '系统设置',
          requiresAuth: true
        }
      },
      {        
        id: 'profile',
        name: 'Profile',
        path: '/profile',
        icon: 'pi pi-user',
        meta: {
          title: '个人中心',
          requiresAuth: true
        }
      },
      {
        id: 'route-test',
        name: 'RouteTest',
        path: '/route-test',
        icon: 'pi pi-test-tube',
        meta: {
          title: '路由测试',
          requiresAuth: true
        }
      }
    ]
    
    // 设置初始激活菜单
    updateActiveMenu(router.currentRoute.value.path)
  }
  
  // 更新激活的菜单项
  const updateActiveMenu = (path: string) => {
    // 查找匹配的菜单项
    const findActiveMenu = (items: MenuItem[]): MenuItem | null => {
      for (const item of items) {
        if (item.path === path) {
          return item
        }
        if (item.children) {
          const found = findActiveMenu(item.children)
          if (found) {
            return found
          }
        }
      }
      return null
    }
    
    const activeItem = findActiveMenu(menus.value)
    if (activeItem) {
      activeMenu.value = activeItem.id
      updateBreadcrumbs(path)
    }
  }
  
  // 更新面包屑
  const updateBreadcrumbs = (path: string) => {
    const breadcrumbPath: MenuItem[] = []
    
    // 递归查找路径
    const findPath = (items: MenuItem[], currentPath: string): boolean => {
      for (const item of items) {
        if (item.path === currentPath) {
          breadcrumbPath.unshift(item)
          return true
        }
        if (item.children) {
          if (findPath(item.children, currentPath)) {
            breadcrumbPath.unshift(item)
            return true
          }
        }
      }
      return false
    }
    
    findPath(menus.value, path)
    breadcrumbs.value = breadcrumbPath
  }
  
  // 根据权限过滤菜单
  const filterMenusByPermission = (_userPermissions: string[]): MenuItem[] => {
    const filterMenu = (items: MenuItem[]): MenuItem[] => {
      return items
        .filter(item => {
          // 如果菜单项不需要认证或用户有对应的权限，则显示
          if (!item.meta?.requiresAuth) return true
          // 这里可以根据实际的权限控制逻辑进行过滤
          return true
        })
        .map(item => {
          // 递归过滤子菜单
          if (item.children) {
            const filteredChildren = filterMenu(item.children)
            // 如果子菜单为空，则不显示该菜单项
            if (filteredChildren.length === 0) {
              return null
            }
            return { ...item, children: filteredChildren }
          }
          return item
        })
        .filter(Boolean) as MenuItem[]
    }
    
    return filterMenu(menus.value)
  }
  
  return {
    menus,
    activeMenu,
    breadcrumbs,
    initMenus,
    updateActiveMenu,
    updateBreadcrumbs,
    filterMenusByPermission
  }
})